#!/bin/bash
# Trigger a run of dkms for all the modules installed
# to ensure we have valid modules build for all.

if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

modules=$(dkms status | tr ',:' ' ' | awk '{ print $1 "/" $2 }')
kernels=$(ls /usr/src/linux-headers-*-*-* -d | sed -e 's|/usr/src/linux-headers-||' || echo "")
# NOTE(bnemec): On Fedora, the versions can be found in /usr/src/kernels
if [ -z "$kernels" ]; then
    kernels=$(ls /usr/src/kernels/* -d | sed -e 's|/usr/src/kernels/||' || echo "")
fi
if [ -z "$kernels" ]; then
    echo "Warning: No kernel versions found for DKMS"
fi
__ARCH=$ARCH
unset ARCH

for module in $modules ; do
    for kernel in $kernels ; do
        dkms build $module -k $kernel
        dkms install $module -k $kernel
    done
done

ARCH=$__ARCH

dkms status
